﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System.Peppers.Win32
{
    /// <summary>
    /// Class that wraps a handle to an unmanaged object, base class for many classes
    /// </summary>
    public abstract class UnmanagedObject : IDisposable
    {
        /// <summary>
        /// Implicitly casts to IntPtr
        /// </summary>
        /// <param name="object">Object to cast</param>
        /// <returns>Intptr, handle</returns>
        public static implicit operator IntPtr(UnmanagedObject @object)
        {
            return @object.Handle;
        }

        /// <summary>
        /// Handle to the unmanaged object
        /// </summary>
        public IntPtr Handle
        {
            get;
            protected set;
        }

        /// <summary>
        /// Calls dispose on finalize, in case it didn't get called
        /// </summary>
        ~UnmanagedObject()
        {
            Dispose();
        }

        #region IDisposable Members

        /// <summary>
        /// Suppresses finalize, and derived classes will delete the handle
        /// </summary>
        public virtual void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
